//
//  OptionScene.m
//  CandyJump
//
//  Created by Jordy on 11年4月3日.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "OptionScene.h"
#import "MenuScene.h"
#import	"AdMobView.h"
#import	"AdDelegate.h"
#import "SimpleAudioEngine.h"

@implementation OptionScene

+ (id) scene
{
	CCScene *scene = [CCScene node];
	
	OptionScene *layer = [OptionScene node];
	
	[scene addChild: layer];
	
	return scene;
}

- (id) init
{
	if ((self = [super init]))
	{
		CCMenuItem *musicToggleButton;
		CCMenuItem *effectToggleButton;
		
		if ([[SimpleAudioEngine sharedEngine] backgroundMusicVolume] > 0.0f)
		{
			isMusicOn = YES;
			musicToggleButton = [CCMenuItemToggle itemWithTarget:self selector:@selector(toggleMusic:) items:[CCMenuItemFont itemFromString: @"Music On"],[CCMenuItemFont itemFromString: @"Music Off"],nil];
		}
		else
		{
			isMusicOn = NO;
			musicToggleButton = [CCMenuItemToggle itemWithTarget:self selector:@selector(toggleMusic:) items:[CCMenuItemFont itemFromString: @"Music Off"],[CCMenuItemFont itemFromString: @"Music On"],nil];
		}

		if ([[SimpleAudioEngine sharedEngine] effectsVolume] > 0.0f)
		{
			isEffectOn = YES;
			effectToggleButton = [CCMenuItemToggle itemWithTarget:self selector:@selector(toggleEffect:) items:[CCMenuItemFont itemFromString: @"Effect On"],[CCMenuItemFont itemFromString: @"Effect Off"],nil];
		}
		else
		{
			isEffectOn = NO;
			effectToggleButton = [CCMenuItemToggle itemWithTarget:self selector:@selector(toggleEffect:) items:[CCMenuItemFont itemFromString: @"Effect Off"],[CCMenuItemFont itemFromString: @"Effect On"],nil];
		}
				
		CCMenuItem *backButton = [CCMenuItemFont itemFromString:@"Back" target:self selector:@selector(gotoMenuScene:)];
		
		CCMenu *menu = [CCMenu menuWithItems: musicToggleButton, effectToggleButton, backButton, nil];
		[menu alignItemsVertically];
	
		[self addChild:menu];
	}
	return self;
}

- (void) gotoMenuScene:(id) sender
{
	[[CCDirector sharedDirector] replaceScene: [CCTransitionFade transitionWithDuration:0.5f scene:[MenuScene scene]]];
}

- (void) toggleMusic:(id) sender
{
	if (isMusicOn)
	{
		[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
		[[SimpleAudioEngine sharedEngine] rewindBackgroundMusic];
		[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:0.0f];
		isMusicOn = NO;
	}
	else
	{
		[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
		[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume:1.0f];
		isMusicOn = YES;
	}

}

- (void) toggleEffect:(id) sender
{
	if (isEffectOn)
	{
		[[SimpleAudioEngine sharedEngine] setEffectsVolume:0.0f];
		isEffectOn = NO;
	}
	else
	{
		[[SimpleAudioEngine sharedEngine] setEffectsVolume:1.0f];
		isEffectOn = YES;
	}
	
}

- (void) dealloc
{
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
- (void) requestFreshAd:(id) sender
{
	[adMobAd requestFreshAd];
}

- (void) onEnter
{
	[super onEnter];
}

- (void) onEnterTransitionDidFinish 		
{
	[super onEnterTransitionDidFinish];
	
	myAdDelegate = [[AdDelegate alloc] init];
	
	adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:myAdDelegate];
	adMobAd.frame = CGRectMake(0, 480, 320, 48); // Hide at the bottom
	[adMobAd retain];
	
	UIViewController *viewController = [myAdDelegate currentViewControllerForAd:adMobAd];
	[viewController.view addSubview:adMobAd];
	
	// Request fresh Ad every 15 seconds
	[self schedule: @selector(requestFreshAd:) interval:15];
}

- (void) onExit
{
	[myAdDelegate hideAdMobView:adMobAd];
	
	[adMobAd removeFromSuperview];
	[adMobAd release];
	
	[myAdDelegate release];
	
	[super onExit];
}

@end
